#!/usr/bin/expect
#初始化远程机目录权限

set timeout 1
set ip [lindex $argv 0]
set root_pass [lindex $argv 1]
set t_user [lindex $argv 2]
set user [lindex $argv 3]
set dir [lindex $argv 4]
set group [lindex $argv 5]
set port [lindex $argv 6]

#开启子进程
spawn ssh ${t_user}@${ip} -p ${port} -i /home/${user}/.Beetle/common_id_rsa
expect "*$*"
send "su\r"
expect "*Password*"
send "${root_pass}\r"
expect "*$*"
send "chown -R ${t_user}:${group} ${dir}\r"
expect {
    "*No such file*" { exit 1 }
}
#expect "*$*"
#send "chmod -R g+rw ${dir}\r"
#expect {
#    "*No such file*" { exit 1 }
#}
#expect "*$*"
#send "setfacl -R -m d:g:${group}:rw ${dir}\r"
#expect {
#    "*No such file*" { exit 1 }
#}
expect "*$*"
send "su ${t_user}\r"
expect "*$*"
send "cd ${dir}\r"
expect {
    "*No such file*" { exit 1 }
}
expect "*$*"
send "mkdir test\r"
expect {
    "*Permission denied*" { exit 1 }
}
#expect "*$*"
#send "ll\r"
expect "*$*"
send "rm -rf test\r"
#expect "*$*"
#send "exit"
#结束子进程
expect eof
exit